package com.hjm.project.riskControl.riskmerblack.controller;

import java.util.Date;
import java.util.List;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.merchantInfo.merchantInfo.domain.MerchantInfo;
import com.hjm.project.merchantInfo.merchantInfo.service.IMerchantInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.riskControl.riskmerblack.domain.Riskmerblack;
import com.hjm.project.riskControl.riskmerblack.service.IRiskmerblackService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 商户黑名单Controller
 * 
 * @author maker-platform
 * @date 2021-11-03
 */
@Controller
@RequestMapping("/riskControl/riskmerblack")
public class RiskmerblackController extends BaseController
{
    private String prefix = "riskControl/riskmerblack";

    @Resource
    private IRiskmerblackService riskmerblackService;

    @Resource
    private IMerchantInfoService iMerchantInfoService;

    @RequiresPermissions("riskControl:riskmerblack:view")
    @GetMapping()
    public String riskmerblack()
    {
        return prefix + "/riskmerblack";
    }

    /**
     * 查询商户黑名单列表
     */
    @RequiresPermissions("riskControl:riskmerblack:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Riskmerblack riskmerblack)
    {
        startPage();
        List<Riskmerblack> list = riskmerblackService.selectRiskmerblackList(riskmerblack);
        return getDataTable(list);
    }

    /**
     * 导出商户黑名单列表
     */
    @RequiresPermissions("riskControl:riskmerblack:export")
    @Log(title = "商户黑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Riskmerblack riskmerblack)
    {
        List<Riskmerblack> list = riskmerblackService.selectRiskmerblackList(riskmerblack);
        ExcelUtil<Riskmerblack> util = new ExcelUtil<Riskmerblack>(Riskmerblack.class);
        return util.exportExcel(list, "riskmerblack");
    }

    /**
     * 新增商户黑名单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户黑名单
     */
    @RequiresPermissions("riskControl:riskmerblack:add")
    @Log(title = "商户黑名单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Riskmerblack riskmerblack) {

        Riskmerblack risk = new Riskmerblack();
        risk.setType("2");
        risk.setTypeVal(riskmerblack.getTypeVal());
        List<Riskmerblack> riskmerblacks = riskmerblackService.selectRiskmerblackList(risk);
        if (CollectionUtils.isNotEmpty(riskmerblacks)) {
            return error("当前身份证已存在,请勿重复添加");
        }

        riskmerblack.setType("2");
        riskmerblack.setStatus("0");
        riskmerblack.setAddMan(ShiroUtils.getSysUser().getLoginName());
        riskmerblack.setAddDate(DateUtils.getDate());
        int i = riskmerblackService.insertRiskmerblack(riskmerblack);
        if (i == 0) {
            return error("录入失败");
        }
        return toAjax(i);
    }

    /**
     * 修改商户黑名单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Riskmerblack riskmerblack = riskmerblackService.selectRiskmerblackById(id);
        mmap.put("riskmerblack", riskmerblack);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户黑名单
     */
    @RequiresPermissions("riskControl:riskmerblack:edit")
    @Log(title = "商户黑名单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Riskmerblack riskmerblack) {
        riskmerblack.setUpdateTime(new Date());
        int i = riskmerblackService.updateRiskmerblack(riskmerblack);
        if (i > 0) {
            if (StringUtils.isNotEmpty(riskmerblack.getMerNum())) {
                MerchantInfo merchantInfo = new MerchantInfo();
                merchantInfo.setMerno(riskmerblack.getMerNum());
                //启用黑名单修改商户为黑名单
                if (riskmerblack.getStatus().equals("0")) {
                    merchantInfo.setMerStatus(Long.parseLong("1"));
                    iMerchantInfoService.updateMerchantInfo(merchantInfo);
                } else {
                    merchantInfo.setMerStatus(Long.parseLong("0"));
                    iMerchantInfoService.updateMerchantInfo(merchantInfo);
                }
            }
        }
        return toAjax(i);
    }

    /**
     * 删除商户黑名单
     */
    @RequiresPermissions("riskControl:riskmerblack:remove")
    @Log(title = "商户黑名单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int i = riskmerblackService.deleteRiskmerblackByIds(ids);
        return toAjax(i);
    }


    /**
     * 前往商户查找带回页面
     */
    @GetMapping("/selectMerInfo")
    public String selectMerInfo() {
        return prefix + "/selectMerInfo";
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Riskmerblack> util = new ExcelUtil<>(Riskmerblack.class);
        return util.importTemplateExcel("商户黑名单模板");
    }

    /**
     * 导入文件
     */
    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importFile(MultipartFile file) throws Exception {
        if (file == null || StringUtils.isEmpty(file.getOriginalFilename())) {
            return error("请上传文件");
        }
        ExcelUtil<Riskmerblack> util = new ExcelUtil<>(Riskmerblack.class);
        List<Riskmerblack> riskmerblacks = util.importExcel(file.getInputStream());
        if (CollectionUtils.isEmpty(riskmerblacks)) {
            return error("文件内容不能为空");
        }
        int i = riskmerblackService.importFile(riskmerblacks);
        return toAjax(i);
    }


}
